from . import AsyncSessionFactory
from .file import FileInfo
from sqlalchemy import select,delete


async def add_file(file: FileInfo):
    async with AsyncSessionFactory() as session:
        async with session.begin():
            session.add(file)

async def get_file_list():
    async with AsyncSessionFactory() as session:
        async with session.begin():
            result = await session.execute(select(FileInfo).order_by(FileInfo.create_time.desc()))
            return result.scalars().all()

async def get_file(file_id: int):
    async with AsyncSessionFactory() as session:
        async with session.begin():
            result = await session.execute(select(FileInfo).where(FileInfo.id == file_id))
            return result.scalar()

async def delete_files(file_id: int):
    async with AsyncSessionFactory() as session:
        async with session.begin():
            await session.execute(delete(FileInfo).where(FileInfo.id == file_id))